/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fh.view;
import fh.view.running.CurrentTourneyOverview;
import fh.component.DataSource;
import fh.controller.PlayerController;
import fh.model.Player;
import fh.view.player.AddPlayer;
import fh.view.player.EditPlayer;
import fh.view.player.DetailPlayer;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Dikkehein
 */
public class PlayerOverview extends javax.swing.JFrame {

    Player aPlayer;
    PlayerController pc;

    /**
     * Creates new form PlayerOverview
     */
    public PlayerOverview() {
        initComponents();

        try {
            DataSource.getConnection();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        aPlayer = new Player();
        pc = new PlayerController();
        FillList();
    }

    private void FillList() {
        DefaultComboBoxModel playerList = pc.getPlayerList();
        PlayerOverviewList.clearSelection();
        PlayerOverviewList.setModel(playerList);
    }

    private Player getPlayer() {


        Player player = (Player) PlayerOverviewList.getSelectedValue();

        // if (player == null) {

        //   JOptionPane.showMessageDialog(null, "Geen speler geselecteerd");



        //}
        return player;



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        CurrnetTourenyOverviewButton = new javax.swing.JButton();
        LocationEventMainButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        PlayerOverviewList = new javax.swing.JList();
        DetailPlayerButton = new javax.swing.JButton();
        EditPlayerButton = new javax.swing.JButton();
        DeletePlayerButton = new javax.swing.JButton();
        AddPlayerButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Speler overzicht");
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 30)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Full House");
        jLabel1.setToolTipText("");

        jLabel3.setText("Speler beheer");

        CurrnetTourenyOverviewButton.setText("Toernooi runnen");
        CurrnetTourenyOverviewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CurrnetTourenyOverviewButtonActionPerformed(evt);
            }
        });

        LocationEventMainButton.setText("Locatie & Event");
        LocationEventMainButton.setMaximumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.setMinimumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LocationEventMainButtonActionPerformed(evt);
            }
        });

        PlayerOverviewList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(PlayerOverviewList);

        DetailPlayerButton.setText("Details");
        DetailPlayerButton.setMaximumSize(new java.awt.Dimension(111, 23));
        DetailPlayerButton.setMinimumSize(new java.awt.Dimension(111, 23));
        DetailPlayerButton.setPreferredSize(new java.awt.Dimension(111, 23));
        DetailPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DetailPlayerButtonActionPerformed(evt);
            }
        });

        EditPlayerButton.setText("Wijzigen");
        EditPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditPlayerButtonActionPerformed(evt);
            }
        });

        DeletePlayerButton.setText("Verwijderen");
        DeletePlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeletePlayerButtonActionPerformed(evt);
            }
        });

        AddPlayerButton.setText("Toevoegen");
        AddPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddPlayerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(DetailPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(EditPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(DeletePlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(AddPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(CurrnetTourenyOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CurrnetTourenyOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeletePlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(EditPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DetailPlayerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CurrnetTourenyOverviewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CurrnetTourenyOverviewButtonActionPerformed
        new CurrentTourneyOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_CurrnetTourenyOverviewButtonActionPerformed

    private void LocationEventMainButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LocationEventMainButtonActionPerformed
        new LocationEventOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_LocationEventMainButtonActionPerformed

    private void EditPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditPlayerButtonActionPerformed
        if (PlayerOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen speler geselecteerd");
        } else {
            aPlayer = getPlayer();
            new EditPlayer(aPlayer).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_EditPlayerButtonActionPerformed

    private void DeletePlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeletePlayerButtonActionPerformed


        if (PlayerOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen speler geselecteerd");
        } else {
            int antwoord = JOptionPane.showConfirmDialog(null, "Wilt u deze speler  verwijderen?\n", "Verwijder speler", JOptionPane.YES_NO_OPTION);
            if (antwoord == JOptionPane.YES_OPTION) {
                aPlayer = getPlayer();
                int result = pc.deletePlayerById(aPlayer.getId());
                if (result >= 0) {
                    FillList();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Er is iets mis gegaan met verwijderen.");
                }
            }

        }
    }//GEN-LAST:event_DeletePlayerButtonActionPerformed

    private void AddPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddPlayerButtonActionPerformed
        new AddPlayer().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_AddPlayerButtonActionPerformed

    private void DetailPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DetailPlayerButtonActionPerformed

        if (PlayerOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen speler geselecteerd");
        } else {
            aPlayer = getPlayer();
            new DetailPlayer(aPlayer).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_DetailPlayerButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayerOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayerOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayerOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayerOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PlayerOverview().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddPlayerButton;
    private javax.swing.JButton CurrnetTourenyOverviewButton;
    private javax.swing.JButton DeletePlayerButton;
    private javax.swing.JButton DetailPlayerButton;
    private javax.swing.JButton EditPlayerButton;
    private javax.swing.JButton LocationEventMainButton;
    private javax.swing.JList PlayerOverviewList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
